//
// Created by hjie on 23-9-3.
//

#include "publish_subscribe_protocol.h"
#include "msg_key.h"
#include <rapidjson/document.h>

PublishReqProtocol::PublishReqProtocol()
{

}

bool PublishReqProtocol::ParseData(rapidjson::Document &input)
{
    rapidjson::Value & content = input[content_k];

    if (content.HasMember(user_id_k) && content[user_id_k].IsString() &&
        content.HasMember(type_k)    && content[type_k].IsString()    &&
        content.HasMember(sdp_k)     && content[sdp_k].IsString())
    {
        m_user_id  = content[user_id_k].GetString();
        m_sdp_type = content[type_k].GetString();
        m_sdp_info = content[sdp_k].GetString();

        if (m_user_id.empty() || m_sdp_type.empty() || m_sdp_info.empty())
        {
            return false;
        }
        return true;
    }
    return false;
}

PublishResProtocol::PublishResProtocol()
{

}

void PublishResProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output,content_k);
}

PublishTargetProtocol::PublishTargetProtocol()
{

}

void PublishTargetProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output, content_k);
    if (m_response_header->m_result == 0)
    {
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), sdp_k, m_sdp_info);
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), type_k, m_sdp_type);
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), from_k, m_from_user_id);
    }
}

SubscribeReqProtocol::SubscribeReqProtocol()
{

}

bool SubscribeReqProtocol::ParseData(rapidjson::Document &input)
{
    rapidjson::Value & content = input[content_k];
    if (content.HasMember(subscribe_user_id_k) && content[subscribe_user_id_k].IsString())
    {
        m_subscribe_user_id = content[subscribe_user_id_k].GetString();
        return true;
    }
    return false;
}


SubscribeResProtocol::SubscribeResProtocol()
{

}

void SubscribeResProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output, content_k);
}

SubscribeTargetProtocol::SubscribeTargetProtocol()
{

}

void SubscribeTargetProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output, content_k);
    if (m_response_header->m_result == 0)
    {
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), subscribe_user_id_k, m_subscribe_user_id);
    }
}

PublishStreamReqProtocol::PublishStreamReqProtocol()
{

}

bool PublishStreamReqProtocol::ParseData(rapidjson::Document &input)
{
    rapidjson::Value & content = input[content_k];
    if (content.HasMember(status_k) && content[status_k].IsBool())
    {
        m_status = content[status_k].GetBool();
        return true;
    }
    return false;
}

PublishStreamResProtocol::PublishStreamResProtocol()
{

}

void PublishStreamResProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output, content_k);
}

GetPublishStreamStatusReqProtocol::GetPublishStreamStatusReqProtocol()
{

}

bool GetPublishStreamStatusReqProtocol::ParseData(rapidjson::Document &input)
{
    rapidjson::Value & content = input[content_k];
    if (content.HasMember(user_id_k) && content[user_id_k].IsString())
    {
        m_user_id = content[user_id_k].GetString();
        if (m_user_id.empty())
        {
            return false;
        }
        return true;
    }
    return false;
}

GetPublishStreamStatusResProtocol::GetPublishStreamStatusResProtocol()
{

}

void GetPublishStreamStatusResProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output, content_k);
    hnhj::AddStringMember(output[content_k], output.GetAllocator(), user_id_k, m_user_id);
    hnhj::AddBoolMember(output[content_k], output.GetAllocator(), status_k, m_publish_status);
}

SendAnswerReqProtocol::SendAnswerReqProtocol()
{

}

bool SendAnswerReqProtocol::ParseData(rapidjson::Document &input)
{
    rapidjson::Value & content = input[content_k];

    if (content.HasMember(user_id_k) && content[user_id_k].IsString() &&
        content.HasMember(type_k)    && content[type_k].IsString()    &&
        content.HasMember(sdp_k)     && content[sdp_k].IsString())
    {
        m_user_id  = content[user_id_k].GetString();
        m_type = content[type_k].GetString();
        m_sdp = content[sdp_k].GetString();

        if (m_user_id.empty() || m_type.empty() || m_sdp.empty())
        {
            return false;
        }
        return true;
    }
    return false;
}

SendAnswerTargetProtocol::SendAnswerTargetProtocol()
{

}

void SendAnswerTargetProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output, content_k);
    if (m_response_header->m_result == 0)
    {
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), sdp_k, m_sdp);
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), type_k, m_type);
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), user_id_k, m_user_id);
    }
}

IceCandidateReqProtocol::IceCandidateReqProtocol()
{

}
/*
 *          "candidate":"candidate:3794970868 1 tcp 1518280447 172.17.0.1 9 typ host tcptype active generation 0 ufrag 8fCW network-id 1",
            "sdpMid":"0",
            "sdpMLineIndex":0,
            "usernameFragment":"8fCW"
 */

bool IceCandidateReqProtocol::ParseData(rapidjson::Document &input)
{
    rapidjson::Value & content = input[content_k];
    if (content.HasMember(user_id_k)   && content[user_id_k].IsString() &&
        content.HasMember(candidate_k) && content[candidate_k].IsObject() &&
        content.HasMember(sdp_type_k)  && content[sdp_type_k].IsString())
    {
        m_user_id  = content[user_id_k].GetString();
        m_sdp_type = content[sdp_type_k].GetString();

        rapidjson::Value & candidate = content[candidate_k];
        if (candidate.HasMember(candidate_k) && candidate[candidate_k].IsString() &&
            candidate.HasMember(sdpMid_k)    && candidate[sdpMid_k].IsString()    &&
            candidate.HasMember(sdpMLineIndex_k)    && candidate[sdpMLineIndex_k].IsInt() &&
            candidate.HasMember(usernameFragment_k) && candidate[usernameFragment_k].IsString())
        {
            m_candidate.m_candidate = candidate[candidate_k].GetString();
            m_candidate.m_sdp_mid   = candidate[sdpMid_k].GetString();
            m_candidate.m_sdp_m_line_index  = candidate[sdpMLineIndex_k].GetInt();
            m_candidate.m_username_fragment = candidate[usernameFragment_k].GetString();
        }
        else
        {
            return false;
        }
        if (m_user_id.empty() || m_sdp_type.empty())
        {
            return false;
        }
        return true;
    }
    return false;
}

IceCandidateTargetProtocol::IceCandidateTargetProtocol()
{

}

void IceCandidateTargetProtocol::Dump(rapidjson::Document &output)
{
    BaseProtocol::Dump(output);
    hnhj::AddObjectMember(output, content_k);
    if (m_response_header->m_result == 0)
    {
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), user_id_k, m_user_id);
        hnhj::AddStringMember(output[content_k], output.GetAllocator(), sdp_type_k, m_sdp_type);

        hnhj::AddObjectMember(output[content_k], output.GetAllocator(), candidate_k);
        rapidjson::Value & candidate = output[content_k][candidate_k];

        hnhj::AddStringMember(candidate, output.GetAllocator(), candidate_k,  m_candidate.m_candidate);
        hnhj::AddStringMember(candidate, output.GetAllocator(), sdpMid_k,     m_candidate.m_sdp_mid);
        hnhj::AddIntMember(candidate, output.GetAllocator(),    sdpMLineIndex_k, m_candidate.m_sdp_m_line_index);
        hnhj::AddStringMember(candidate, output.GetAllocator(), usernameFragment_k, m_candidate.m_username_fragment);
    }
}









